package com.A21.GuoSai.demo.workflow_adjust.service;

import com.A21.GuoSai.demo.atom_adjust.service.AtomPojoService;
import org.camunda.feel.FeelEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scala.util.Either;
import scala.util.Left;
import scala.util.Right;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FeelEngineService {

    @Autowired
    private FeelEngine feelEngine;
    @Autowired
    private WorkFlowPojoService workFlowPojoService;


    private String judge(String judgetext, Map<String,Object> varibles){
        Either<FeelEngine.Failure, Object> failureObjectEither = feelEngine.evalExpression(judgetext, varibles);
        if (failureObjectEither instanceof Left) {
            return  ((Left)failureObjectEither).value().toString();
        }
        return "legalJudge";
        /*else {
            return  ((Right)failureObjectEither).value().toString();
        }*/
    }


    public List<String> judgeWorker(String flowId,String zeebeWorkerName,Map<String,Object>variables){
        List<String>expressions = workFlowPojoService.getWorkerExpressionFlowMessage(flowId,zeebeWorkerName);
        List<String>failmessages=new ArrayList<>();
        /*expressions.forEach(
                expression->{
                    String result=judge(expression,variables);
                    if(!result.equals("legalJudge"))
                        failmessages.add("工作流--"+flowId+"--原子服务"+zeebeWorkerName+"发生错误:"+result);
                }
        );*/
        for(String expression:expressions){
            String result=judge(expression,variables);
            if(!result.equals("legalJudge"))
                failmessages.add("工作流--"+flowId+"--原子服务"+zeebeWorkerName+"发生错误:"+result);
        }

        return failmessages;
    }
}
